package com.ddxcloud.movie.controller;

import com.ddxcloud.common.annotation.RepeatSubmit;
import com.ddxcloud.common.constants.enums.OrderStatusEnum;
import com.ddxcloud.movie.data.dto.RefundDTO;
import com.ddxcloud.movie.service.AlipayService;
import com.ddxcloud.movie.service.WechatRefundService;
import com.github.binarywang.wxpay.bean.result.WxPayRefundV3Result;
import com.github.binarywang.wxpay.exception.WxPayException;
import io.swagger.annotations.Api;
import io.swagger.v3.oas.annotations.Operation;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.Map;

@RestController
@RequestMapping("/refund")
@Api(tags = "退款")
public class RefundController {

    @Resource
    AlipayService alipayService;

    @Resource
    WechatRefundService wechatRefundService;

    /**
     * 支付宝-手动退款
     * @param dto
     * @param request
     */
    @RepeatSubmit
    @Operation(summary = "支付宝-手动退款")
    @PostMapping("/alipay/refund")
    public void refund(@RequestBody RefundDTO dto, HttpServletRequest request) {
        alipayService.refund(dto, request);
    }

    /**
     * 微信支付-退款回调
     * @param request
     * @param response
     * @return
     * @throws WxPayException
     * @throws IOException
     */
    @Operation(summary = "微信支付-退款回调", hidden = true)
    @PostMapping("/wechat/notify")
    public Map<String, String> wechatNotify(HttpServletRequest request, HttpServletResponse response) throws WxPayException, IOException {
        return wechatRefundService.notify(request, response);
    }

    @GetMapping("/orderRefund")
    public String orderRefund(@RequestParam("orderId") String orderId, @RequestParam("refundAmount") Integer refundAmount, HttpServletRequest request) {
        RefundDTO refund=new RefundDTO();
        refund.setOrderId(orderId);
        refund.setRefundAmount(refundAmount);
        refund.setReason("手动退款");
        refund.setOrderStatus(OrderStatusEnum.STATUS_4.getCode());
        WxPayRefundV3Result order = wechatRefundService.createOrder(refund, request);
        System.out.println(orderId+":退款结果返回:"+order);
        return "退款成功";
    }
}
